<?php

    #-------------------------------------------------------------#
	# This file is part of the Tekuna MVC Microframework for PHP. #
	# (c) 2009 Niels Jäckel <niels.jaeckel@tekuna.org>            #
	# Please visit http://tekuna.org/ for more information.       #
	#                                                             #
	# This software is licensed under the MIT License. Please     #
	# view the LICENSE file for the full text of this license.    #
    #-------------------------------------------------------------#


	Tekuna :: load('org.tekuna.core.request.ClientInformation');


	class ClientInformationTest extends AbstractTekunaTestCase {

		public function testInstantiation() {

			// init SERVER array
			$_SERVER['REMOTE_ADDR'] = '12.34.56.78';
			$_SERVER['REMOTE_PORT'] = '1234';
			$_SERVER['HTTP_USER_AGENT'] = 'TestClient/1.0';
			$_SERVER['HTTP_ACCEPT'] = 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
			$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'de-de,de;q=0.8,en-us;q=0.5,en;q=0.3';
			$_SERVER['HTTP_ACCEPT_ENCODING'] = 'gzip,deflate';
			$_SERVER['HTTP_ACCEPT_CHARSET'] = 'ISO-8859-1,utf-8;q=0.7,*;q=0.7';

			// instantiate the object
			$objCI = new ClientInformation();

			// check the imported values
			$this -> assertIdentical($objCI -> getIP(), '12.34.56.78');
			$this -> assertIdentical($objCI -> getPort(), 1234);
			$this -> assertIdentical($objCI -> getUserAgent(), 'TestClient/1.0');
			$this -> assertIdentical($objCI -> getAcceptedMimeTypes(), Array ('text/html', 'application/xhtml+xml', 'application/xml', '*/*'));
			$this -> assertIdentical($objCI -> getAcceptedLanguages(), array('de-de', 'de', 'en-us', 'en'));
			$this -> assertIdentical($objCI -> getAcceptedEncodings(), array('gzip', 'deflate'));
			$this -> assertIdentical($objCI -> getAcceptedCharsets(), array('iso-8859-1', 'utf-8', '*'));

			// check accept matches
			$this -> assertTrue($objCI -> isMimeTypeAccepted('text/html'));
			$this -> assertTrue($objCI -> isMimeTypeAccepted('application/xml'));
			$this -> assertTrue($objCI -> isMimeTypeAccepted('foo/bar'));
			$this -> assertFalse($objCI -> isMimeTypeAccepted('notgood'));

			$this -> assertTrue($objCI -> isLanguageAccepted('de'));
			$this -> assertTrue($objCI -> isLanguageAccepted('en-us'));
			$this -> assertFalse($objCI -> isLanguageAccepted('fr'));

			$this -> assertTrue($objCI -> isEncodingAccepted('gzip'));
			$this -> assertFalse($objCI -> isEncodingAccepted('gzip2'));

			$this -> assertTrue($objCI -> isCharsetAccepted('utf-8'));
			$this -> assertTrue($objCI -> isCharsetAccepted('iso-8859-15'));
		}


		public function testIPParsing() {

			// init SERVER array
			$_SERVER['REMOTE_ADDR'] = '12.34.56.789';
			$_SERVER['REMOTE_PORT'] = '1234';
			$_SERVER['HTTP_USER_AGENT'] = 'TestClient/1.0';
			$_SERVER['HTTP_ACCEPT'] = 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
			$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'de-de,de;q=0.8,en-us;q=0.5,en;q=0.3';
			$_SERVER['HTTP_ACCEPT_ENCODING'] = 'gzip,deflate';
			$_SERVER['HTTP_ACCEPT_CHARSET'] = 'ISO-8859-1,utf-8;q=0.7,*;q=0.7';

			// instantiate the object
			$objCI = new ClientInformation();

			// the IP was corrected and does not equal the original value
			$this -> assertNotIdentical($objCI -> getIP(), '12.34.56.789');
		}
	}



